package com.veetao.api.utils;

import java.util.Properties;

import org.apache.commons.lang3.math.NumberUtils;

public class ConfigUtils {

    // private static final Log logger = LogFactory.getLog(ConfigUtils.class);

    // 静态块的加载顺序 为书写顺序 sysConfig.load需要放在前面
    private static final Properties sysConfig = new Properties();

    static {
        // try {
        // InputStream iStream =
        // ConfigUtils.class.getResourceAsStream("sys.properties"); // 相对于classpath路径
        // sysConfig.load(iStream);
        // } catch (Exception e) {
        // logger.info("ConfigUtils.static load sys.properties error", e);
        // }
    }

    public static final String CDN_DOMAIN = getCdnDomain();

    public static final int CACHE_TIME = getCacheTime();

    // 开启DEBUG_MODE时,不验证ticket和sig值，用户身分由传入的hostId指定
    public static final String DEBUG_MODE = getDebugMode();

    public static String getPropertyValue(String key) {
        return sysConfig.getProperty(key);
    }

    private static int getCacheTime() {
        int cacheTime = NumberUtils.toInt(getPropertyValue("CACHE_TIME")); // sys.properties里配置的变量
        if (cacheTime == 0)
            cacheTime = NumberUtils.toInt(System.getProperty("CACHE_TIME")); // jvm环境变量
        if (cacheTime == 0)
            cacheTime = NumberUtils.toInt(System.getenv("CACHE_TIME")); // 系统环境变量

        return cacheTime;
    }

    private static final String getCdnDomain() {
        String domain = getPropertyValue("CDN_DOMAIN"); // sys.properties里配置的变量
        if (domain == null)
            domain = System.getProperty("CDN_DOMAIN"); // jvm环境变量
        if (domain == null)
            domain = System.getenv("CDN_DOMAIN"); // 系统环境变量

        if (domain == null) {
            return "";
        }
        return domain.trim().toLowerCase();
    }

    private static final String getDebugMode() {
        String debugMode = System.getProperty("DEBUG_MODE"); // jvm环境变量
        if (debugMode == null)
            debugMode = System.getenv("DEBUG_MODE"); // 系统环境变量

        if (debugMode == null) {
            return "";
        }
        return debugMode.trim().toLowerCase();
    }

    public static final boolean getRequestFastMode() {
        String requestMode = System.getProperty("REQUEST_MODE"); // 系统环境变量
        return "true".equals(requestMode);
    }

    public static final boolean getMonitorMode() {
        String monitorMode = System.getProperty("MONITOR_MODE"); // 系统环境变量
        if (null != monitorMode) {
            if (monitorMode.equals("true")) {
                return true;

            }
        }
        return false;

    }

    public static boolean isProductEnv() {
        String profile = System.getProperty("spring.profiles.active");
        return "product".equals(profile);
    }

    public static boolean isPreparePublishEnv() {
        String profile = System.getProperty("spring.profiles.active");
        return "preparepublish".equals(profile);
    }

    public static boolean isDevelopmentEnv() {
        String profile = System.getProperty("spring.profiles.active");
        return "dev".equals(profile);
    }

    public static boolean isTestEnv() {
        String profile = System.getProperty("spring.profiles.active");
        return "test".equals(profile);
    }

    public static final boolean getRegistMode() {
        String registMode = System.getProperty("REGIST_MODE"); // 系统环境变量
        if (null != registMode) {
            if (registMode.equals("true")) {
                return true;
            }
        }
        return false;

    }

    public static final boolean isCongestionControl() {
        String isControl = getPropertyValue("Block_Control");
        if (isControl.equals("true")) {
            return true;
        } else {
            return false;
        }
    }

    public static int warnBlockThreads() {

        String key = "warnBlockThreads";
        String threads = ConfigUtils.getPropertyValue(key);
        if (null != threads) {
            int number = NumberUtils.toInt(threads);
            return number;
        }
        return 40;

    }

    public static int requestNumPerMinutes() {
        String key = "requestNumPerMinutes";
        String threads = ConfigUtils.getPropertyValue(key);
        if (null != threads) {
            int number = NumberUtils.toInt(threads);
            return number;
        }
        return 60;
    }

    /**
     * @return
     * @author guohongtao
     */
    public static String getActiveProfile() {
        return System.getProperty("spring.profiles.active");
    }
}
